﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using General.Core.Data;
using General.Entities.Data;
using General.Entities.Models;
using General.Entities.ViewData.Admin;
using Microsoft.EntityFrameworkCore;

namespace General.Services.Admin
{
    public class AdminService:IAdminService
    {
        private readonly IRepository<tc_admin> tc_AdminRepository;
        private readonly IRepository<tc_role> tc_RoleRepository;

        public AdminService(IRepository<tc_admin> tc_adminRepository,
            IRepository<tc_role> tc_roleRepository)
        {
            tc_AdminRepository = tc_adminRepository;
            tc_RoleRepository = tc_roleRepository;
        }

        /// <summary>
        /// 根据编号获取用户信息
        /// </summary>
        /// <returns></returns>
        public async Task<tc_admin> GetInfoById(int id)
        {
            var model = await tc_AdminRepository.getByIdAsync(id);
            return model;
        }

        /// <summary>
        /// 根据用户名获取用户信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public async Task<tc_admin> GetInfoByUserName(string name)
        {
            var model = await tc_AdminRepository.GetAsync(x=>x.username == name);
            return model;
        }

        /// <summary>
        /// 更新登录时间信息
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public async Task UpdateLoginStatusInfo(int uid,string ip)
        {
            var user = await tc_AdminRepository.getByIdAsync(uid);

            user.lastip = ip;
            user.lasttime = DateTime.Now;
            await tc_AdminRepository.UpdateAsync(user);
        }

        /// <summary>
        /// 修改头像
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="headimg"></param>
        /// <returns></returns>
        public async Task UpdateHeadImg(int uid, string headimg)
        {
            var user = await tc_AdminRepository.getByIdAsync(uid);
            user.headimg = headimg;

            await tc_AdminRepository.UpdateAsync(user);
        }

        /// <summary>
        /// 修改用户名称
        /// </summary>
        /// <returns></returns>
        public async Task UpdateUsername(tc_admin model, string username)
        {
            model.username = username;

            await tc_AdminRepository.UpdateAsync(model);
        }

        /// <summary>
        /// 修改用户密码
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public async Task UpdateUserPwd(int uid, string pwd)
        {
            var user = await tc_AdminRepository.getByIdAsync(uid);
            user.password = EncryptHelper.Md5(pwd);

            await tc_AdminRepository.UpdateAsync(user);
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <param name="username"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public async Task<AdminResult> GetList(int page, int limit, string username, int status=-1)
        {
            AdminResult result = new AdminResult();
            var query = from d in tc_AdminRepository.Table
                        where !string.IsNullOrWhiteSpace(username)?d.username.Contains(username):d.id>=0
                        orderby d.id descending
                        select new AdminModel()
                        {
                            id = d.id,
                            createtime = d.createtime,
                             username = d.username, headimg = d.headimg, lastip = d.lastip, lasttime =d.lasttime,   role = d.role , status = d.status, roles = new List<tc_role>()
                        };
            if (status!=-1)
            {
                query = query.Where(x=>x.status == status);
            }
            result.count = await query.CountAsync();
            //获取所有角色列表
            var rolelist = await tc_RoleRepository.GetAllAsync().OrderBy(x=>x.id).ToListAsync();
            var list = await query.Skip((page - 1) * limit).Take(limit).ToListAsync();
            foreach (var item in list)
            {
                if (string.IsNullOrEmpty(item.role))
                {
                    continue;
                }
                var rolearr = item.role.Split(",",StringSplitOptions.RemoveEmptyEntries).Select<string, int>(x => Convert.ToInt32(x));
                item.roles = rolelist.Where(x=>rolearr.Contains(x.id)).ToList();
            }
            result.data = list;
            result.code = 0;
            return result;
        }


        /// <summary>
        /// 增加用户
        /// </summary>
        /// <param name="username"></param>
        /// <param name="status"></param>
        /// <param name="role"></param>
        /// <returns></returns>
        public async Task<int> AddInfo(string username, int status, string role)
        {
            var model = new tc_admin()
            {
                 createtime = DateTime.Now, headimg = "/images/user.png", lastip = "", lasttime = DateTime.Now,
                  password = EncryptHelper.Md5("123456"), role = role, status = status, username = username
            };
            return await tc_AdminRepository.insertAsync(model);
        }

        /// <summary>
        /// 编辑用户
        /// </summary>
        /// <returns></returns>
        public async Task EditInfo(tc_admin data)
        {
            var model = await tc_AdminRepository.getByIdAsync(data.id);
            model.role = data.role;
            model.username = data.username;
            model.status = data.status;
            await tc_AdminRepository.UpdateAsync(model);
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <returns></returns>
        public async Task<int> DeleteInfo(string ids)
        {
            var arr = ids.Split(",", StringSplitOptions.RemoveEmptyEntries);
            var arrints = arr.Select<string, int>(x => Convert.ToInt32(x));
            var list = await tc_AdminRepository.GetAllAsync(x => arrints.Contains(x.id)).ToListAsync();
            return await tc_AdminRepository.DeleteListAsync(list);
        }

        /// <summary>
        /// 编辑用户状态
        /// </summary>
        /// <returns></returns>
        public async Task<int> EditStatus(int id,int status)
        {
            var user = await tc_AdminRepository.getByIdAsync(id);
            user.status = status;
            return await tc_AdminRepository.UpdateAsync(user);
        }

    }
}
